#define _CRT_SECURE_NO_WARNINGS 1
#include"String.h"
namespace Celia
{
	const size_t string::npos = -1;

	string::string(const char* ptr)
	{
		_size = strlen(ptr);
		_capacity = _size;
		_str = new char[_capacity + 1];
		strcpy(_str, ptr);
	}
	string::string(const string& ptr)
	{
		/*_size = ptr._size;
		_capacity = ptr._capacity;
		_str = new char[ptr._capacity + 1];
		strcpy(_str, ptr._str);*/
		string tmp(ptr._str);
		swap(tmp);
	}
	string::~string()
	{
		delete[] _str;
		_str = nullptr;
		_size = _capacity = 0;
	}
	void string::clear()
	{
		_str[0] = '\0';
		_size = 0;
	}
	const char* string::c_str()const
	{
		return _str;
	}
	void string::push_back(char c)
	{
		if (_size == _capacity)
		{
			size_t newcapacity = _capacity == 0 ? 4 : 2 * _capacity;
			char* ptr = new char[newcapacity + 1];
			strcpy(ptr, _str);
			delete[] _str;
			_str = ptr;
			ptr = nullptr;
			_capacity = newcapacity;
		}
		_str[_size++] = c;
		_str[_size] = '\0';
	}
	string& string::operator+=(char c)
	{
		push_back(c);
		return *this;
	}
	void string::append(const char* str)
	{
		size_t len = strlen(str);
		if (_size + len >= _capacity)
		{
			size_t newcapacity = _size + len < 2 * _capacity ? 2 * _capacity : _size + len;
			char* ptr = new char[newcapacity + 1];
			strcpy(ptr, _str);
			delete[] _str;
			_str = ptr;
			ptr = nullptr;
			_capacity = newcapacity;
		}
		for (int i = 0; i < len; i++)
		{
			_str[_size + i] = str[i];
		}
		_size += len;
		_str[_size] = '\0';
	}
	void string::swap(string& s)
	{
		/*string tmp = *this;
		std::swap(_size, s._size);
		std::swap(_capacity, s._capacity);
		delete[] _str;
		_str = new char[_capacity];
		strcpy(_str, s._str);
		delete[] s._str;
		s._str = new char[s._capacity];
		strcpy(s._str, tmp._str);*/

		std::swap(_str, s._str);
		std::swap(_capacity, s._capacity);
		std::swap(_size, s._size);
	}
	size_t string::size() const
	{
		return _size;
	}
	size_t string::capacity() const
	{
		return _capacity;
	}
	bool string::empty() const
	{
		return _size == 0;
	}
	void string::resize(size_t n, char c)
	{
		if (n < _size)
		{
			_str[n] = '\0';
			_size = n;
		}
		else
		{
			if (n >= _capacity)
			{
				size_t newcapacity = n < 2 * _capacity ? 2 * _capacity : n;
				char* ptr = new char[newcapacity + 1];
				strcpy(ptr, _str);
				delete[] _str;
				_str = ptr;
				ptr = nullptr;
				_capacity = newcapacity;
			}
			for (int i = 0; i < n - _size; i++)
			{
				_str[_size + i] = c;
			}
			_str[n] = '\0';
			_size = n;
		}
	}
	void string::reserve(size_t n)
	{
		if (n > _capacity)
		{
			char* tmp = new char[n];
			strcpy(tmp, _str);
			delete[] _str;
			_str = tmp;
			tmp = nullptr;
			_capacity = n;
		}
	}
	size_t string::find(char c, size_t pos) const
	{
		assert(pos < _size);
		string::iterator begin = this->begin();
		string::iterator end = this->end();
		begin += pos;
		while (begin < end)
		{
			if (*begin == c)
			{
				return begin - this->begin();
			}
			begin++;
		}
		return string::npos;
	}
	size_t string::find(const char* s, size_t pos ) const
	{
		assert(pos < _size);
		char* tmp = strstr(_str + pos, s);
		if (tmp != nullptr)
			return tmp - this->begin();
		return string::npos;
	}
	string& string::insert(size_t pos, char c)
	{
		assert(pos < _size + 1);
		if (_size == _capacity)
		{
			size_t newcapacity = _capacity == 0 ? 4 : 2 * _capacity;
			char* ptr = new char[newcapacity + 1];
			strcpy(ptr, _str);
			delete[] _str;
			_str = ptr;
			ptr = nullptr;
			_capacity = newcapacity;
		}
		//012  3456
		//abc \0
		size_t end = _size + 1;
		while (end > pos)
		{
			_str[end] = _str[end - 1];
			end--;
		}
		_str[end] = c;
		_size++;
		return *this;
	}
	string& string::insert(size_t pos, const char* str)
	{
		assert(pos < _size + 1);
		size_t len = strlen(str);
		if (_size + len >= _capacity)
		{
			size_t newcapacity = _size + len < 2 * _capacity ? 2 * _capacity : _size + len;
			char* ptr = new char[newcapacity + 1];
			strcpy(ptr, _str);
			delete[] _str;
			_str = ptr;
			ptr = nullptr;
			_capacity = newcapacity;
		}
		//abc\0            xxx
		//012 3456
		size_t end = _size + len;
		while (end >= pos + len)
		{
			_str[end] = _str[end - len];
			end--;
		}
		for (int i = 0; i < len; i++)
		{
			_str[pos + i] = str[i];
		}
		_size += len;
		return *this;
	}
	string& string::erase(size_t pos, size_t len)
	{
		assert(pos < _size);
		if (len >= _size - pos)
		{
			_size = pos;
			_str[pos] = '\0';
		}
		else
		{
			//01234 5678  
			//abcde\0
			size_t end = pos + len;
			while (end <= _size)
			{
				_str[end - len] = _str[end];
				end++;
			}
		}
		return *this;
	}
	string& string::operator+=(const char* str)
	{
		append(str);
		return *this;
	}
	char& string:: operator[](size_t index)
	{
		assert(index < _size);
		return _str[index];
	}

	const char& string::operator[](size_t index)const
	{
		assert(index < _size);
		return _str[index];
	}
	string::iterator string::begin() const
	{
		return &_str[0];
	}
	string::iterator string::end() const
	{
		return &_str[_size];
	}
	bool string::operator<(const string& s)
	{
		return strcmp(_str, s._str) < 0;
	}
	bool string::operator==(const string& s)
	{
		return strcmp(_str, s._str) == 0;
	}
	bool string::operator<=(const string& s)
	{
		return *this == s || *this < s;
	}

	bool string::operator>(const string& s)
	{
		return !(*this <= s);
	}

	bool string::operator>=(const string& s)
	{
		return !(*this < s);
	}
	bool string::operator!=(const string& s)
	{
		return !(*this == s);
	}
	std::istream& operator>>(std::istream& in, string& s)
	{
		s.clear();

		const int N = 256;
		char buff[N] = { 0 };
		char ch = 0;
		int i = 0;

		ch = in.get();
		while (ch != '\n')
		{
			buff[i++] = ch;
			if (i == N - 1)
			{
				buff[i] = '\0';
				s += buff;
				i = 0;
			}
			ch = in.get();
		}
		if (i > 0)
		{
			buff[i] = '\0';
			s += buff;
		}
		return in;
	}
	std::ostream& operator<<(std::ostream& out, string& s)
	{
		for (int i = 0; i < s.size(); i++)
		{
			out << s[i];
		}
		out << std::endl;
		return out;
	}
}